﻿Option Explicit On
Module modReg

    'Public regList As Collection
    Public regLists As New ArrayList

    Public Function ConvertStringToLines(ByVal org As String) As String()
        Return org.Split(vbLf)
    End Function

    Public Class RegClass

        Public Structure RegUnit
            Public UnitType As ArrayList
            Public UnitName As ArrayList
        End Structure

        Public Function GetFinalNum(ByVal SectionArray As KeyValue.INISection(), ByVal SectionName As String) As Integer
            Dim cTmpClass As New KeyValue
            Dim Lines As String()
            Dim RegSectionContext As String = cTmpClass.GetSectionContextFormName(SectionArray, SectionName)
            Dim curNum As Integer = 0
            Dim curLinetmp As String

            Lines = ConvertStringToLines(RegSectionContext)

            For Each Line In Lines
                curLinetmp = Line.Split(";")(0)
                curLinetmp = Trim(curLinetmp.Split("=")(0))
                curLinetmp = Replace(curLinetmp, Chr(9), "")

                Try
                    If curLinetmp <> "" And curLinetmp <> Nothing And curLinetmp <> " " And InStr(curLinetmp, "]") = 0 Then
                        If CInt(curLinetmp) > 0 Then curNum = CInt(curLinetmp) 'curNum + 1
                    End If
                Catch ex As Exception
                    'MsgBox(ex.Message)
                End Try

            Next

            Return curNum

        End Function

        Public Function GetRegInfoArray(ByVal SectionArray As KeyValue.INISection()) As RegUnit
            regLists.Add("InfantryTypes")
            regLists.Add("VehicleTypes")
            regLists.Add("AircraftTypes")
            regLists.Add("BuildingTypes")
            regLists.Add("SuperWeaponTypes")
            regLists.Add("VoxelAnims")
            regLists.Add("Particles")
            regLists.Add("ParticleSystems")
            regLists.Add("Warheads")
            regLists.Add("Animations")
            regLists.Add("ArmorTypes")
            regLists.Add("WeaponTypes")
            regLists.Add("TerrainTypes")
            regLists.Add("SmudgeTypes")
            regLists.Add("Countries")

            Dim Lines As String()
            Dim tmpLine As String
            Dim tmpReg As RegUnit
            tmpReg.UnitType = New ArrayList
            tmpReg.UnitName = New ArrayList

            For Each List As String In regLists
                tmpLine = cSection.GetSectionContextFormName(SectionArray, List)
                If tmpLine <> "SYSTEM:NOTHING" Then
                    Lines = cSection.GetSectionContextFormName(SectionArray, List).Split(vbLf)
                    tmpLine = ""
                    For Each Line In Lines
                        tmpLine = Line
                        tmpLine = tmpLine.Split(";")(0)
                        'Try
                        If UBound(tmpLine.Split("=")) >= 1 Then
                            tmpLine = Trim(tmpLine.Split("=")(1)).Replace("" & Chr(9) & "", " ") '.Replace("		", "")
                            tmpLine = Trim(tmpLine.Split(Chr(9))(0))
                        Else
                            tmpLine = ""
                        End If
                        'Catch ex As Exception
                        'tmpLine = ""
                        'End Try
                        If tmpLine <> "" Then
                            For Each Section In SectionArray
                                If tmpLine = Section.SectionName + vbCr Or tmpLine = Section.SectionName + vbLf Or tmpLine = Section.SectionName + vbCrLf Or tmpLine = Section.SectionName + " " Or tmpLine = Section.SectionName Then
                                    tmpReg.UnitType.Add(Section.SectionName)
                                    'tmpReg.UnitType.Add(List)
                                    tmpReg.UnitName.Add(tmpLine)
                                End If
                            Next
                        End If
                    Next
                End If
            Next

            Return tmpReg

        End Function

        Public Function GetSectionRegInfo(ByVal RegArray As RegUnit, ByVal SectionName As String) As String()
            Dim cIndex As Integer = -1
            Dim tmpStrReturn(1) As String
            'cIndex = RegArray.UnitName.

            For Each Unit In RegArray.UnitName
                cIndex = cIndex + 1
                If Trim(Unit) = Trim(SectionName) Then Exit For
            Next

            If cIndex >= 0 Then
                tmpStrReturn(0) = RegArray.UnitType(cIndex)
            Else
                tmpStrReturn(0) = "SYSTEM:UNKNOWN"
            End If
            tmpStrReturn(1) = SectionName
            Return tmpStrReturn
        End Function

        Public Function ProduceRegStr(ByVal OrgStr As String()) As String

            If UBound(OrgStr) = 1 Then
                Dim tmpStr As String = Trim(OrgStr(0))
                Dim tmpStr2 As String = Trim(OrgStr(1))
                If OrgStr(0) = "SYSTEM:UNKNOWN" Then
                    tmpStr = "Unknown" + " -> " + tmpStr2
                Else
                    tmpStr = "Registered -> " + tmpStr + " -> " + tmpStr2
                End If
                Return tmpStr
            Else
                Return "Unknown" + " -> " + "UnableToRecognize"
            End If

        End Function

        Public Sub GeneralRegNewUnit(ByVal ParentForm As frmMain, ByVal RegName As String, ByVal RegListName As String)
            Dim RegSectionContext As String = cSection.GetSectionContextFormName(ParentForm.iSection, RegListName)
            Dim RSCLines As New ArrayList
            Dim RSCLinesArray As String() = ConvertStringToLines(RegSectionContext)
            Dim tmpLine As String
            Dim i As Integer = -1
            Dim num As Integer = cReg.GetFinalNum(ParentForm.iSection, RegListName)

            For Each Line In RSCLinesArray
                i = i + 1

                tmpLine = Line

                tmpLine = tmpLine.Split(";")(0)
                tmpLine = Trim(tmpLine.Split("=")(0))
                If tmpLine = CStr(num) Then
                    RSCLines.Add(Line)
                    RSCLines.Add(CStr(num + 1) + "=" + RegName)
                Else
                    RSCLines.Add(Line)
                End If

            Next

            RegSectionContext = ""
            For Each Line As String In RSCLines
                If RegSectionContext = "" Then
                    RegSectionContext = Line
                Else
                    RegSectionContext = RegSectionContext + vbLf + Line
                End If
            Next

            ParentForm.iSection(cSection.GetSectionIndexFormName(ParentForm.iSection, RegListName)).SectionContext = RegSectionContext
        End Sub

    End Class

End Module
